package com.payment.ktb.activity.main4;

import android.app.DatePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.flyco.animation.BounceEnter.BounceTopEnter;
import com.flyco.animation.SlideExit.SlideTopExit;
import com.flyco.dialog.widget.popup.base.BasePopup;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.adapter.OrderManageAdapter;
import com.payment.ktb.constants.ConstantsOrderManage;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.model.OrderManageEntity;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by admin on 2017/12/4.
 */

public class OrderManageResearchActivity extends BaseActivity {


    @BindView(R.id.et_ordermanageresearch_userName)
    EditText et_ordermanageresearch_userName;
    @BindView(R.id.btn_ordermanageresearch_research)
    Button btn_ordermanageresearch_research;
    @BindView(R.id.prlv_ordermanageresearch)
    PullToRefreshListView prlv_ordermanageresearch;
    @BindView(R.id.rl_ordermanageresrearch_nocustomer)
    RelativeLayout rl_ordermanageresrearch_nocustomer;



    private int page = 0;
    private List<OrderManageEntity> listDatas = new ArrayList<>();
    OrderManageAdapter adapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ordermanageresearch);
        initTitle("订单管理");
        ButterKnife.bind(this);
        initData();

        prlv_ordermanageresearch.setMode(PullToRefreshBase.Mode.BOTH);
        prlv_ordermanageresearch.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                //下拉刷新
                if (checkUserAccount()) {
                    pullDownToRefresh();
                }
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                pullUptoLoad();
            }
        });


    }
    private void initData() {

        // 设置下拉刷新加载文本
        prlv_ordermanageresearch.getLoadingLayoutProxy(true, false).setPullLabel("下拉刷新...");
        prlv_ordermanageresearch.getLoadingLayoutProxy(true, false).setReleaseLabel("放开刷新...");
        prlv_ordermanageresearch.getLoadingLayoutProxy(true, false).setRefreshingLabel("正在加载...");
        // 设置上拉加载文本
        prlv_ordermanageresearch.getLoadingLayoutProxy(false, true).setPullLabel("上拉刷新...");
        prlv_ordermanageresearch.getLoadingLayoutProxy(false, true).setReleaseLabel("放开刷新...");
        prlv_ordermanageresearch.getLoadingLayoutProxy(false, true).setRefreshingLabel("正在加载...");
    }

    private boolean checkUserAccount() {
        if (!TextUtils.isEmpty(et_ordermanageresearch_userName.getText().toString().trim())) {
            return true;
        }else {
            ToastUtils.showToast("请先输入商户名称");
            return false;
        }
    }



    private void pullDownToRefresh() {

        progressDialogUtils.showProgress();
        //构造传输参数
        page = 0;
        Map<String, String> map = new HashMap<String,String>();

        map.put("userName", et_ordermanageresearch_userName.getText().toString().trim());
        map.put("limit", "20");
        map.put("page", String.valueOf(page));
        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.OrgChildUserOrder, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {

                try {
                    JSONObject jsonObject = new JSONObject(response);

                    progressDialogUtils.dismissProgress();
                    prlv_ordermanageresearch.onRefreshComplete();
                    listDatas.clear();

                    //data
                    listDatas = new Gson().fromJson(jsonObject.getJSONArray("records").toString(), new TypeToken<List<OrderManageEntity>>() {}.getType());
                    if (listDatas == null || listDatas.isEmpty()) {
                        rl_ordermanageresrearch_nocustomer.setVisibility(View.VISIBLE);
                        prlv_ordermanageresearch.setVisibility(View.GONE);
                    }else {
                        rl_ordermanageresrearch_nocustomer.setVisibility(View.GONE);
                        prlv_ordermanageresearch.setVisibility(View.VISIBLE);
                        //adapter
                        adapter = new OrderManageAdapter(mContext, listDatas);
                        //bind
                        prlv_ordermanageresearch.setAdapter(adapter);
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }

            }
            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                prlv_ordermanageresearch.onRefreshComplete();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });
    }






    private void pullUptoLoad() {

        progressDialogUtils.showProgress();
        //构造传输参数
        page = page + 1;
        Map<String, String> map = new HashMap<String,String>();

        map.put("userName", et_ordermanageresearch_userName.getText().toString());
        map.put("limit", "20");
        map.put("page", String.valueOf(page));
        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.OrgChildUserOrder, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {

                try {

                    JSONObject jsonObject = new JSONObject(response);

                    progressDialogUtils.dismissProgress();
                    prlv_ordermanageresearch.onRefreshComplete();
                    //data
                    List<OrderManageEntity> orderManages = null;
                    orderManages = new Gson().fromJson(jsonObject.getJSONArray("records").toString(), new TypeToken<List<OrderManageEntity>>() {}.getType());

                    if (orderManages != null && !orderManages.isEmpty()) {
                        //adapter
                        listDatas.addAll(orderManages);
                        adapter.notifyDataSetChanged();
                    }else {
                        ToastUtils.showToast("没有更多数据啦！");
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }

            }
            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                prlv_ordermanageresearch.onRefreshComplete();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });

    }


    @OnClick({R.id.btn_ordermanageresearch_research})
    public void clickEvent(View view) {

        switch (view.getId()) {

            //搜索
            case R.id.btn_ordermanageresearch_research:
               if (checkUserAccount()) {
                   pullDownToRefresh();
               }
                break;
        }
    }


}
